﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitView;

namespace YJKRevitWndr
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWinInsert : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			HideWarningAndErroringKit @object = new HideWarningAndErroringKit(new List<string>
			{
				"WinInsert"
			}, HideWarningAndErroringKit.HideKitEnum.HideWarning);
			Result result;
			try
			{
				using (DlgWinDoorInsert dlgWinDoorInsert = new DlgWinDoorInsert(cmdData.Application, false))
				{
					try
					{
						while (dlgWinDoorInsert.ShowDialog() == DialogResult.OK)
						{
							ImpDoorWinInsert impDoorWinInsert = new ImpDoorWinInsert(this.m_uiApp, dlgWinDoorInsert.m_datum);
							FamilySymbol type;
							if (dlgWinDoorInsert.rdoFamilyLib.Checked)
							{
								type = dlgWinDoorInsert.m_datum.m_symbol.Create1(this.m_doc);
								Transaction transaction = new Transaction(this.m_doc, "WinInsert");
								transaction.Start();
								cmdData.Application.Application.FailuresProcessing += @object.OnFailuresProcessing;
								transaction.Commit();
							}
							else
							{
								type = dlgWinDoorInsert.familySymbolDoc;
							}
							if (dlgWinDoorInsert.AddTag)
							{
								Family family = dlgWinDoorInsert.TagDbFamily.Reload(this.m_doc);
								FamilySymbol tagFamilysymbol = null;
								List<FamilySymbol> symbols = family.GetSymbols();
								if (symbols.Count > 0)
								{
									tagFamilysymbol = symbols[0];
								}
								impDoorWinInsert.AddTag = true;
								impDoorWinInsert.TagFamilysymbol = tagFamilysymbol;
								impDoorWinInsert.FirstTagString = dlgWinDoorInsert.FirstTagString;
								impDoorWinInsert.SecondTagString = dlgWinDoorInsert.SecondTagString;
							}
							switch (dlgWinDoorInsert.m_nInsertType)
							{
                            case InsertType.eByPickPoint:
                            impDoorWinInsert.PickPtInsert(BuiltInCategory.OST_Windows, type, dlgWinDoorInsert.cbxHeight.Text);
                            break;
                            case InsertType.eByEqual:
                            impDoorWinInsert.EqualInsert(BuiltInCategory.OST_Windows, type, Convert.ToDouble(dlgWinDoorInsert.cbxHeight.Text), ref message);
                            break;
                            case InsertType.eByButtress:
                            impDoorWinInsert.ButtressInsert(BuiltInCategory.OST_Windows, type, Convert.ToDouble(dlgWinDoorInsert.cbxHeight.Text), ref message);
                            break;
                            case InsertType.eByGridLineEqual:
                            impDoorWinInsert.GridLineEqualInsert(BuiltInCategory.OST_Windows, type, Convert.ToDouble(dlgWinDoorInsert.cbxHeight.Text), ref message);
                            break;
							}
						}
						dlgWinDoorInsert.CloseDocuments();
					}
					catch (Exception)
					{
					}
					result = base.YJKExcute(cmdData, ref message, elements);
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = base.YJKExcute(cmdData, ref message, elements);
			}
			return result;
		}
	}
}
